<%@ page import="com.summer.pojo.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
User user= (User) request.getSession().getAttribute("user");
%>
<!doctype html>
<html>
<head>
    <meta charset='utf-8'>
    <title>订购页面</title>
    <link rel="stylesheet" href="/static/css/style2.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div class="box" id="app">
    <h2>Order</h2>
    <form method="post" action="">
        <div class="inputBox">
            <input type="text" name="bookname" required="" value="${book1.bookname}">
            <label>书籍名称</label>
        </div>
        <div class="inputBox">
            <input type="text" name="bookauthor" required="" value="${book1.bookauthor}">
            <label>书籍作者</label>
        </div>
        <div class="inputBox">
            <input type="text" name="bookprice" required="" value="${book1.bookprice}">
            <label>书籍价格</label>
        </div>
        <div class="inputBox">
        <input type="text" name="book" required=""   class="bookcount" id="bookcount">
            <label>书籍数量</label>
        </div>
        <div class="inputBox">
            <input type="text" name="book" required=""  value="" class="bookmoney" id="bookmoney">
            <label>书籍总价格</label>
        </div>
        <input type="button" name="" value="购买" onclick="by()">&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="${pageContext.request.contextPath}/book/userfindAllBook"><input type="button" value="返回书城!" /></a>

    </form>

</div>

</body>
<script>
    var countt=${book1.bookcount}
    var bookid=${book1.bookid}
    var price=${book1.bookprice}
    var userid=<%=user.getId()%>
    $('.bookcount').on('input',function(){
        $('.bookmoney').val(($(this).val())*price);
    })
    function by() {
        var count =document.getElementById("bookcount").value;
        var money =document.getElementById("bookmoney").value;
        var counts=countt-count;
        alert(counts);
        if (count==""){
            alert("请输入书籍数量");

        }else {
            if (!(/(^[1-9]\d*$)/.test(count))) {
                alert("输入的不是正整数");
            }else {
                if(confirm("确认购买吗？")) {
                    $.ajax({
                        url: "/order/addOrder",
                        type: "POST",
                        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                        data: {
                            "uid":userid,
                            "bookid":bookid,
                            "count":count,
                            "money":money,
                            "counts":counts,
                        },
                        success: function () {
                            alert("购买订单生成成功,请到订单页面查看!");
                            window.location = "/userOrder";
                        },
                        error: function () {
                            alert("订单生成失败,请重新购买!");
                            <%--window.location="${pageContext.request.contextPath}/book/userfindBookById?bookid=${book.bookid}";--%>
                        }
                    })
                }
            }
        }

    }
</script>
</html>